import { TableCustom } from '@vh-admin/pro-components';
import { useModel } from '@umijs/max';
import { columnsFn } from './define';
import { roleCreate, roleDel, roleEdit, roleIndex } from '@/services/sys/role';
import access from '@/access';
import type { SubmitOnDoneType } from '@vh-admin/pro-components/lib/t-table-custom/types';
import { message } from 'antd';
const { checkPermissionAuth } = access();

function Role() {
  const { globalInfo, requestDictionaries } = useModel('useGlobalModel');

  return (
    <>
      <TableCustom<any>
        request={roleIndex}
        columns={globalInfo ? columnsFn(globalInfo.dictionaries) : []}
        headerTitleConfig={{
          create: {
            auth: () => {
              return checkPermissionAuth({ accessId: 20 });
            },
            text: '新增角色',
            modalConfig: {
              modalType: 'Form',
              edit: false,
              config: {
                title: '新增角色',
                submitRequest: roleCreate,
                submitOnDone: ({ status }: SubmitOnDoneType) => {
                  if (status === 'success') {
                    requestDictionaries();
                    message.success('新增角色成功');
                  }
                },
              },
            },
          },
        }}
        operationConfig={{
          edit: {
            auth: () => {
              return checkPermissionAuth({ accessId: 20 });
            },
            modalConfig: {
              modalType: 'Form',
              edit: true,
              config: {
                title: '编辑角色',
                submitRequest: roleEdit,
                submitOnDone: ({ status }: SubmitOnDoneType) => {
                  if (status === 'success') {
                    requestDictionaries();
                    message.success('编辑角色成功');
                  }
                },
              },
            },
          },
          delete: {
            auth: () => {
              return checkPermissionAuth({ accessId: 21 });
            },
            modalConfig: {
              modalType: 'Delete',
              edit: true,
              config: {
                title: '删除角色',
                submitRequest: roleDel,
                submitOnDone: ({ status }: SubmitOnDoneType) => {
                  if (status === 'success') {
                    requestDictionaries();
                    message.success('删除角色成功');
                  }
                },
              },
            },
          },
        }}
      />
    </>
  );
}

export default Role;
